/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.action;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sourceforge.perforce.core.IP4TeamProvider;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.internal.P4TeamProvider;
import net.sourceforge.perforce.ui.PerforceUIPlugin;
import net.sourceforge.perforce.ui.internal.Policy;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class MarkResolvedAction extends PerforceAction {
  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public void run(IAction action) {
    run(new IRunnableWithProgress() {
      public void run(IProgressMonitor monitor) throws InvocationTargetException {
        try {
          Map<RepositoryProvider, List<IResource>> map = getProviderMapping();
          Set<RepositoryProvider> keySet = map.keySet();

          monitor.beginTask("", keySet.size() * 1000);
          monitor.setTaskName("Marking as resolved...");

          MultiStatus status =
              new MultiStatus(PerforceUIPlugin.ID, MultiStatus.OK,
                  "There were one or more problems perfoming 'Mark As Resolved'", null);

          for (Iterator<RepositoryProvider> it = keySet.iterator(); it.hasNext()
              && !monitor.isCanceled();) {
            IProgressMonitor subMonitor = Policy.subMonitorFor(monitor, 1000);

            IP4TeamProvider provider = (IP4TeamProvider) it.next();
            List<IResource> list = map.get(provider);
            IResource[] providerResources = list.toArray(new IResource[list.size()]);

            IStatus tempStatus = provider.markAsResolved(providerResources, subMonitor);
            status.merge(tempStatus);
          }

          if (!status.isOK()) {
            throw new InvocationTargetException(new PerforceException(status));
          }
        } catch (TeamException e) {
          throw new InvocationTargetException(e);
        }
      }

    }, PROGRESS_DIALOG);
  }

  // ---------------------------------------------------------------------------
  // Protected Methods

  @SuppressWarnings("unused")
  // PerforceException
  @Override
  protected boolean isEnabled() throws PerforceException {
    IResource[] resources = getSelectedResources();
    if (resources.length == 0) {
      return false;
    }

    for (int i = 0; i < resources.length; i++) {
      RepositoryProvider tempProvider = RepositoryProvider.getProvider(resources[i].getProject());
      if (tempProvider == null || !tempProvider.getID().equals(PerforceCorePlugin.getTypeId())) {
        return false;
      }

      P4TeamProvider provider = (P4TeamProvider) tempProvider;
      if (!provider.isCheckedOut(resources[i])) {
        return false;
      }

      if (!provider.hasRemote(resources[i])) {
        return false;
      }

      if (provider.isResolved(resources[i])) {
        return false;
      }
    }

    return true;
  }
}
